<?php
class Jenis_ujian extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
	}
	
	function index($kode_mp,$id_sekolah,$id_ta,$act='')
	{
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		if($this->session->userdata('roleid')=='0') //diknas
			$colModel['NAMA_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		$colModel['NAMA_TA'] = array('Tahun Ajaran',100,TRUE,'center',1);
		$colModel['NAMA_MATA_PELAJARAN'] = array('Mata Pelajaran',100,TRUE,'center',1);
		$colModel['KODE_UJIAN'] = array('Kode Ujian',70,TRUE,'center',1);
		$colModel['NAMA_UJIAN'] = array('Nama Ujian',300,TRUE,'center',1);

		$colModel['PROSENTASE'] = array('Prosentase (%)',70,TRUE,'center',0);
		$colModel['DESKRIPSI'] = array('Deskripsi',300,TRUE,'center',0);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Jenis Pengujian'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$jml_persen = $this->pelajaran_model->getPersen($id_sekolah, $id_ta, $kode_mp)->row()->jml;
		if($jml_persen < 100)
			$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Back to Pelajaran','back','org_js');
		
		//Build js
		$grid_js = build_grid_js('flex1',site_url("/jenis_ujian/listjenisujian_grid/".$kode_mp."/".$id_sekolah."/".$id_ta),$colModel,'no','asc',$gridParams,$buttons);
				
		if($jml_persen < 100){
			$data['header_info'] = '
			<div>
			<font size="2" color="red"><b>Jumlah Prosentase Pengujian Masih Kurang dari 100%</b></font>
			</div>
			';
		}else{
			$data['header_info'] = '
			<div>
			<font size="2" color="green"><b>Jumlah Prosentase Pengujian 100%</b></font>
			</div>
			';
		}
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] .= '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
			
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					 location.href='".base_url()."index.php/jenis_ujian/input_jenisujian/".$kode_mp."/".$id_sekolah."/".$id_ta."'; 
				}
				
				if (com=='Back to Pelajaran')
				{
					location.href='".base_url()."index.php/ms_pelajaran/'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/jenis_ujian/delete_jenisujian/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta)."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
								location.href='".base_url()."index.php/jenis_ujian/index/".$kode_mp."/".$id_sekolah."/".$id_ta."';
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
			
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listjenisujian_grid($kode_mp,$id_sekolah,$id_ta)
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		// if($this->session->userdata('roleid')=='0') //diknas
			// $valid_fields = array('NAMA_SEKOLAH','NAMA_TA','KODE_MATA_PELAJARAN','NAMA_MATA_PELAJARAN','KET');
		// else	
			$valid_fields = array('NAMA_TA','pel.NAMA_MATA_PELAJARAN','an.ASPEK_PENILAIAN','p.KODE_UJIAN','p.NAMA_UJIAN','p.PROSENTASE');
		
		$this->flexigrid->validate_post('p.ID_JENIS_UJIAN','asc',$valid_fields);
		
		$records = $this->pelajaran_model->get_jenisujian($kode_mp,$id_sekolah,$id_ta);
		
		// var_dump($records);
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			if($this->session->userdata('roleid')=='0'){ //diknas
				$record_items[] = array($row->ID_JENIS_UJIAN,
				$no+1,
				'<div align="left">'.$row->NAMA_SEKOLAH.'</div>',
				'<div align="left">'.$row->NAMA_TA .'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN .'</div>',
				'<div align="left">'.$row->KODE_UJIAN .'</div>',
				'<div align="left">'.$row->NAMA_UJIAN .'</div>',
				'<div align="left">'.$row->PROSENTASE .'</div>',
				'<div align="left">'.$row->DESKRIPSI .'</div>',
				'<a href=\''.site_url().'/jenis_ujian/edit_jenisujian/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/'.$row->KODE_UJIAN.'/'.$row->ID_JENIS_UJIAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}else{
				$record_items[] = array($row->ID_JENIS_UJIAN,
				$no+1,
				'<div align="left">'.$row->NAMA_TA .'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN .'</div>',
				'<div align="left">'.$row->KODE_UJIAN .'</div>',
				'<div align="left">'.$row->NAMA_UJIAN .'</div>',
				'<div align="left">'.$row->PROSENTASE .'</div>',
				'<div align="left">'.$row->DESKRIPSI .'</div>',
				'<a href=\''.site_url().'/jenis_ujian/edit_jenisujian/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/'.$row->KODE_UJIAN.'/'.$row->ID_JENIS_UJIAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('kode_ujian', 'Kode Ujian', 'required');
			$this->form_validation->set_rules('nama_ujian', 'Nama Ujian', 'required');
			$this->form_validation->set_rules('prosentase', 'Prosentase', 'required|numeric');
			if($this->session->userdata('roleid')=='0') //diknas
				$this->form_validation->set_rules('nama_sekolah', 'Nama Sekolah', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function cek_jenisujian($kode_mp,$id_sekolah,$thn_ajaran,$kode_ujian){
		$rs_cek = $this->pelajaran_model->validasi_jenisujian($kode_mp,$id_sekolah,$thn_ajaran, $kode_ujian)->row();
		return $rs_cek;
	}
	
	function input_jenisujian($kode_mp,$id_sekolah,$id_ta)
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
			$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			
			$data['content'] = $this->load->view('form_jenisujian',$data,true);
			$this->load->view('main',$data);
		}
		else{
			if($this->cek_jenisujian($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('kode_ujian'))->jml > 0 ){
				$data['status'] = 'new';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				
				$data['value'] = $this->input->post();
				$data['err_msg'] = 'Kode Ujian telah ada dalam database.';
				$data['content'] = $this->load->view('form_jenisujian',$data,true);
				$this->load->view('main',$data);
			}else{
				$jml_persen = $this->pelajaran_model->getPersen($id_sekolah, $id_ta, $kode_mp)->row()->jml;
				if($jml_persen + $this->input->post('prosentase') > 100){
					$data['status'] = 'new';
					$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
					$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
					$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
					
					$data['value'] = $this->input->post();
					$data['err_msg'] = 'Jumlah Prosentase Melebihi 100%.';
					$data['content'] = $this->load->view('form_jenisujian',$data,true);
					$this->load->view('main',$data);
				}else{
					$this->insert_jenisujian($kode_mp,$id_sekolah,$id_ta);
				}
			}
		}            
    }
	
	function insert_jenisujian($kode_mp,$id_sekolah,$id_ta){
		$data = array(
			'TAHUN_AJARAN'			=> $id_ta,
			'ID_SEKOLAH'			=> $id_sekolah,
			'KODE_MATA_PELAJARAN'	=> $kode_mp,	
			'KODE_UJIAN'			=> $this->input->post('kode_ujian'),	
			'NAMA_UJIAN'			=> $this->input->post('nama_ujian'),	
			'PROSENTASE'			=> $this->input->post('prosentase'),	
			'DESKRIPSI'				=> $this->input->post('deskripsi')
		);
		$this->pelajaran_model->insert_jenisujian($data);
		
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('jenis_ujian/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	function edit_jenisujian($kode_mp,$id_sekolah,$id_ta,$kode_ujian,$id_ujian)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta."#".$id_ujian;
			$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			
			$data['rs_jenisujian'] = $this->pelajaran_model->get_jenisujianID($id_ujian);
			$data['content'] = $this->load->view('form_jenisujian',$data,true);
			$this->load->view('main',$data);
		}
		else{
			if($this->cek_jenisujian($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('kode_ujian'))->jml > 0 and $this->cek_jenisujian($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('kode_ujian'))->ID_JENIS_UJIAN != $id_ujian){
				$data['status'] = 'edit';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta."#".$id_ujian;
				$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				
				$data['rs_jenisujian'] = $this->pelajaran_model->get_jenisujianID($id_ujian);
				$data['err_msg'] = 'Kode Mata Pelajaran telah ada dalam database.';
				$data['content'] = $this->load->view('form_jenisujian',$data,true);
				$this->load->view('main',$data);
				
			}else{
				$jml_persen = $this->pelajaran_model->getPersen($id_sekolah, $id_ta, $kode_mp,$id_ujian)->row()->jml;
				//echo $jml_persen." ".$jml_persen;exit;
				if($jml_persen + $this->input->post('prosentase') > 100){
					$data['status'] = 'edit';
					$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta."#".$id_ujian;
					$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
					$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
					
					$data['rs_jenisujian'] = $this->pelajaran_model->get_jenisujianID($id_ujian);
					// $data['value'] = $this->input->post();
					$data['err_msg'] = 'Jumlah Prosentase Melebihi 100%.';
					$data['content'] = $this->load->view('form_jenisujian',$data,true);
					$this->load->view('main',$data);
				}else{
					$this->update_jenisujian($kode_mp,$id_sekolah,$id_ta,$kode_ujian,$id_ujian);
				}
			}
		}            
    }
	
	function update_jenisujian($kode_mp,$id_sekolah,$id_ta,$kode_ujian,$id_ujian){
		// if($this->session->userdata('roleid')=='0'){ //diknas
			// $nama_sekolah = $this->input->post('nama_sekolah');
		// }else{
			// $nama_sekolah = $nama_sekolahp;
		// }
		$data = array(
			'TAHUN_AJARAN'			=> $id_ta,
			'ID_SEKOLAH'			=> $id_sekolah,
			'KODE_MATA_PELAJARAN'	=> $kode_mp,	
			'KODE_UJIAN'			=> $this->input->post('kode_ujian'),	
			'NAMA_UJIAN'			=> $this->input->post('nama_ujian'),	
			'PROSENTASE'			=> $this->input->post('prosentase'),	
			'DESKRIPSI'				=> $this->input->post('deskripsi')
		);
		$this->pelajaran_model->update_jenisujian($data, $id_ujian);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('jenis_ujian/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	//Delete
	function delete_jenisujian($kode_mp,$id_sekolah,$id_ta)
	{
		// $this->pelajaran_model->delete_grading($kode_mp, $id_sekolah,$id_ta,$kode_penilaian);
		// $this->session->set_flashdata('suc_msg', 'Data berhasil dihapus.');
		// redirect('grading/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta);
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->pelajaran_model->delete_jenisujian($ta_id);
				$msg .= "Data Jenis Ujian berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
	
}
?>
